package ex.datastream.windows.keyedVsNonKeyedWindows;

import ex.datastream.ApiFrame;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.windowing.assigners.ProcessingTimeSessionWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;

/**
 * Keyed Windows
 */
public class KeyedWindow extends ApiFrame {
    public static void main(String[] args) throws Exception {
        KeyedWindow windows=new KeyedWindow();
        windows.getEnv();

        SingleOutputStreamOperator<Tuple2<String,Long>> outputStreamOperator =windows.getSingleOutputStreamOperator();

        //有keyBy
        KeyedStream<Tuple2<String,Long>, String> keyedStream = outputStreamOperator.keyBy(value -> value.f0);

        WindowedStream<Tuple2<String,Long>, String, TimeWindow> windowedStream1 = keyedStream.window(ProcessingTimeSessionWindows.withGap(Time.seconds(5)));

        windowedStream1.sum(1).print();

        windows.env.execute();
    }
}
